package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.R;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.entity.ShoppingCart;
import com.example.reggie.service.SetmealService;
import com.example.reggie.service.impl.DishServiceImpl;
import com.example.reggie.service.impl.SetmealServiceImpl;
import com.example.reggie.service.impl.ShoppingCartServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/*购物车*/
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartServiceImpl service;


    /*查询购物车*/
    @GetMapping("/list")
    public R list(HttpServletRequest request){
        Long user = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ShoppingCart::getUserId,user);
        List<ShoppingCart> list = service.list(wrapper);
        return R.success(list);
    }


    /*添加*/
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request){
        Long user = (Long) request.getSession().getAttribute("user");
        shoppingCart.setUserId(user);

//        shoppingCart.setAmount(1);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,user);

        /*判断点的是菜品还是套餐*/
        if(shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = service.getOne(queryWrapper);


        if (one==null){
            shoppingCart.setNumber(1);

//            shoppingCart.setAmount(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())));

            shoppingCart.setCreateTime(LocalDateTime.now());
            service.save(shoppingCart);
        }else {
            one.setNumber(one.getNumber()+1);
            service.updateById(one);
        }

        return R.success("添加成功");
    }


    /*取消购物车*/
    @PostMapping("/sub")
    public R sub( @RequestBody ShoppingCart shoppingCart,HttpServletRequest request){
        Long user = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,user);


        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:"+shoppingCart);


        System.out.println("1111111111111111111111111111111111111111111111 "+shoppingCart.getNumber());

        if(shoppingCart.getDishId()!=null){
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //select * from shoppingcart where userid=? and dishId=?

        ShoppingCart one = service.getOne(wrapper);

        Integer number = one.getNumber();

        System.out.println("1asddddddddddddddddddddddda"+number);

        if (one.getNumber()>1){
            one.setNumber(one.getNumber()-1);
            service.updateById(one);
        }else {
            one.setNumber(0);
            service.removeById(one.getId());
        }
        return R.success(one);
    }


    @DeleteMapping("/clean")
    public R clean(HttpServletRequest request){
        Long user = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,user);
        service.remove(wrapper);
        return R.success("删除成功");
    }
}
